"""
内容：ROC曲线与P-R曲线(sklearn包实现)
日期：2020年6月28日
作者：Howie
"""
from numpy import array
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc, precision_recall_curve, average_precision_score

class_F = array([0, 0, 0, 0, 1, 1, 0, 0, 1, 1])
proba_F = array([0.05, 0.15, 0.15, 0.25, 0.34, 0.45, 0.55, 0.65, 0.95, 0.95])

class_G = array([0, 0, 1, 0, 1, 0, 0, 1, 0, 1])
proba_G = array([0.05, 0.05, 0.15, 0.25, 0.35, 0.45, 0.65, 0.75, 0.85, 0.95])

false_positive_rate_F, true_positive_F, thresholds_F = roc_curve(
    class_F, proba_F)
false_positive_rate_G, true_positive_G, thresholds_G = roc_curve(
    class_G, proba_G)
roc_auc_F = auc(false_positive_rate_F, true_positive_F)
roc_auc_G = auc(false_positive_rate_G, true_positive_G)

plt.figure(0)
plt.title('Receiver Operating Characteristic')
plt.xlabel('False Positive Rate(1 - Specificity)')
plt.ylabel('True Positive Rate(Sensitivity)')

plt.plot(
    false_positive_rate_F,
    true_positive_F,
    'b',
    label='Model F (AUC = %0.2f)' %
    roc_auc_F)
plt.plot(
    false_positive_rate_G,
    true_positive_G,
    'g',
    label='Model G (AUC = %0.2f)' %
    roc_auc_G)

plt.plot([0, 1], [1, 1], 'y--')
plt.plot([0, 1], [0, 1], 'r--')

plt.legend(loc='lower right')

plt.savefig('./logs/ROC Curve(with sklearn).pdf')
plt.show()

precision_F, recall_F, _ = precision_recall_curve(class_F, proba_F)
precision_G, recall_G, _ = precision_recall_curve(class_G, proba_G)

ap_F = average_precision_score(class_F, proba_F)
ap_G = average_precision_score(class_G, proba_G)

plt.figure(1)
plt.title('Precision-Recall Graph')
plt.xlabel('Recall')
plt.ylabel('Precision')

plt.plot(recall_F, precision_F, 'b', label='Model F (AP = %0.2f)' % ap_F)
plt.plot(recall_G, precision_G, 'g', label='Model G (AP = %0.2f)' % ap_G)

plt.legend(loc='upper right')

plt.savefig('./logs/P-R Curve(with sklearn).pdf')
plt.show()
